import { wxPromise } from '../gh-promisify/gh-promisify.js';
import regeneratorRuntime from '../../vendor/regenerator-runtime/runtime.js';

const pro = wxPromise();

class GHImage {
  constructor() {
  }
  /**
    保存图片到系统相册
    图片文件路径，可以是临时文件路径或永久文件路径
    需要用户授权 scope.writePhotosAlbum
  */
  static async saveToPhotosAlbum(filePath) {
    let res = await pro.getSetting();
    if (!res) {
      return {code:1, msg: 'get setting failed!'}
    }

    if (res.authSetting['scope.writePhotosAlbum']) {
      res = await pro.saveImageToPhotosAlbum({filePath});
      return {code:0,msg: 'success'};
    }
    
    res = await pro.authorize({ scope: 'scope.writePhotosAlbum'});
    if (!res) {
      res = await pro.saveImageToPhotosAlbum({ filePath });
      return { code: 0, msg: 'success' };
    }

    res = pro.showModal({
      title: '温馨提示',
      content: '请授权系统使用保存图片接口',
      confirmText: '知道了',
      showCancel: true
    });

    if (res.confirm) {
      pro.openSetting();
    }
  }

  static saveToFile(filePath) {
  }
}

export default GHImage;